package tools.map_creator;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;


public class MapCreator
{

	private static final int BOMBITA 			 = -10240;
	private static final int LOPEZ_REGGAE		 = -14254336;
	private static final int LOPEZ_REGGAE_ALADO	 = -65316;
	private static final int CECILIO			 = -16767233;
	private static final int PARED_CEMENTO		 = -12566464;
	private static final int PARED_LADRILLOS	 = -65536;
	private static final int PARED_ACERO		 = -8355712;
	//private static final int FONDO				 = -16777216;
	
	private static final String readPath 		= "/images/maps/";
	private static final String writePath 		= "";
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException
	{
		
		List<String> mapNames = new ArrayList<String>();
		File directory = new File ("src");
		
		mapNames.add("level1");
		mapNames.add("level2");
		mapNames.add("level3");
		mapNames.add("level4");
		mapNames.add("level5");
		
		for( String mapName : mapNames ) {		
			
			File imageFile = new File( directory+readPath+mapName+".bmp" ); 
			
			BufferedImage bufferedImage = ImageIO.read(imageFile);  
			int width = bufferedImage.getWidth();
			int height = bufferedImage.getHeight(null);
			
			//Get Pixels
			int [] rgbs = new int[width*height];
			bufferedImage.getRGB(0, 0, width, height, rgbs, 0, width); //Get all pixels
			
			PrintWriter mapFile = new PrintWriter(new FileWriter(writePath+mapName+".xml"));
			
			String player = "";
			String npcs = "\n\t<NPC>\n";
			String paredes = "\n\t<Wall>\n";
			
			int offset = 0 ;
			int j;
			
			for(int i=0;i<height;i++) {
				
				for(j=0;j<width;j++) {		
					
					int index = i+j+offset;
					//System.out.println("rgbs["+i+", "+j+"]= "+rgbs[index]);
					switch(rgbs[index]) {
						case BOMBITA: 
							player = player+"\t<Player x=\""+j+"\" y=\""+i+"\" />\n";
							break;
						case LOPEZ_REGGAE: 
							npcs = npcs+"\t\t<business_logic.game_elements.LopezReggae x=\""+j+".0\" y=\""+i+".0\"/>\n";
							break;
						case LOPEZ_REGGAE_ALADO: 
							npcs = npcs+"\t\t<business_logic.game_elements.LopezReggaeAlado x=\""+j+".0\" y=\""+i+".0\"/>\n";
							break;
						case CECILIO: 
							npcs = npcs+"\t\t<business_logic.game_elements.Cecilio x=\""+j+".0\" y=\""+i+".0\"/>\n";
							break;
						case PARED_CEMENTO: 
							paredes = paredes+"\t\t<business_logic.game_elements.BloqueCemento x=\""+j+".0\" y=\""+i+".0\"/>\n";
							break;
						case PARED_LADRILLOS: 
							paredes = paredes+"\t\t<business_logic.game_elements.BloqueLadrillos x=\""+j+".0\" y=\""+i+".0\"/>\n";
							break;
						case PARED_ACERO: 
							paredes = paredes+"\t\t<business_logic.game_elements.BloqueAcero x=\""+j+".0\" y=\""+i+".0\"/>\n";
							break;
					}
				}
				
				offset = offset + j - 1;
			}
			
			npcs = npcs+"\t</NPC>\n";
			paredes = paredes+"\t</Wall>";
			
			mapFile.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<Level>\n\n");
			mapFile.print(player+npcs+paredes+"\n\n");
			mapFile.print( "\t<Drop>\n"+
						        "\t\t<business_logic.game_elements.Timer/>\n"+
						        "\t\t<business_logic.game_elements.UpgradeToleTole/>\n"+
						        "\t\t<business_logic.game_elements.Chala/>\n"+
						        "\t\t<business_logic.game_elements.Timer/>\n"+
						        "\t\t<business_logic.game_elements.UpgradeToleTole/>\n"+
						        "\t\t<business_logic.game_elements.Chala/>\n"+
						        "\t\t<business_logic.game_elements.Timer/>\n"+
						        "\t\t<business_logic.game_elements.UpgradeToleTole/>\n"+
						        "\t\t<business_logic.game_elements.Chala/>\n"+
						    "\t</Drop>\n\n"+
					
						   "\t<Score max=\"150\" win_bonus=\"500\" />\n\n"+

   						   "<Info version=\"1\" />"+
					
						"</Level>\n");
			
			mapFile.close();
		}
	}

}
